﻿using System;
using System.Linq;
using System.Web.Mvc;
using Framework.Util.Cryptography;
using MvcContrib.Pagination;
using Project.DAL.Repository;
using Project.Domain.Entities;

namespace Project.WebUI.Areas.Admin.Controllers
{
    public class ManageUsersController : Controller
    {
        #region "private"
        private readonly IUserRepository _userRepository;
        private readonly IDepositRepository _depositRepository;
        private readonly IWithDrawRepository _withDrawRepository;
        private readonly IUserService _userService;
        private readonly ICountryRepository _countryRepository;
        private readonly IContactRepository _contactRepository;        
        private readonly IStoreSecureRepositoty _storeSecureRepositoty;

        public ManageUsersController(
            IUserRepository userRepository,
            IDepositRepository depositRepository,
            IWithDrawRepository withDrawRepository,
            IUserService userService,
            ICountryRepository countryRepository,
            IContactRepository contactRepository,            
            IStoreSecureRepositoty storeSecureRepositoty)
        {
            _userRepository = userRepository;
            _depositRepository = depositRepository;
            _withDrawRepository = withDrawRepository;
            _userService = userService;
            _countryRepository = countryRepository;
            _contactRepository = contactRepository;            
            _storeSecureRepositoty = storeSecureRepositoty;
        }

        #endregion        

        #region "LoadAjax"
        public ActionResult LoadAjax(int page)
        {            
            var listusers = _userRepository.GetMany(u => u.IsAdmin == false,
                                                    o => o.OrderByDescending(t => t.CreatedTime));
            ViewBag.result = listusers.Count();
            if(page==1)
            {
                ViewBag.start = 0;
            }
            else
            {
                ViewBag.start = (page-1) * 10;
            }            

            return PartialView("_MainContent", listusers.AsPagination(page, 10));
        }
        #endregion

        #region "UpdateUser"
        public ActionResult UpdateUser(User user)
        {
            var userUpdate = _userRepository.GetById(user.UserId);
            userUpdate.UserName = user.UserName;
            userUpdate.Password = CryptographyManager.Encrypt(user.Password);
            _userRepository.SaveChanges();
            return View();
        }
        #endregion

        #region "UpdateStatus"
        public ActionResult UpdateStatus(int id, int status)
        {
            var user = _userRepository.GetUserById(id);
            if (status == 1)
            {
                user.Active = false;
                _userRepository.SaveChanges();
                ViewBag.status = false;
                ViewBag.id = id;
                ViewBag.controller = "manageusers";
                return PartialView("_UpdateStatus");
            }
            user.Active = true;
            _userRepository.SaveChanges();
            ViewBag.status = true;
            ViewBag.id = id;
            ViewBag.controller = "manageusers";
            return PartialView("_UpdateStatus");
        }

        #endregion

        #region "DeleteAjax"
        public ActionResult DeleteAjax(int id)
        {
            var user = _userRepository.GetById(id);
            try
            {
                _userRepository.Delete(user);
                _userRepository.SaveChanges();
                return Content("1");
            }
            catch (Exception)
            {

                return Content("0");
            }            
        }
        #endregion               

        #region "Listallwithdraw"
        public ActionResult Listallwithdraw(int? page)
        {
            var listwithdraw =_withDrawRepository.GetMany(null, d => d.OrderByDescending(t => t.CreatedTime).ThenBy(t => t.Amount));
            return View(listwithdraw.AsPagination(page ?? 1, 10));
        }
        #endregion

        #region "EditUser"
        public ActionResult EditUser(int userid)
        {
            var user = _userRepository.GetById(userid);
            user.Active = true;
            user.Balance = 1;            
            return RedirectToAction("alluser");
        }
        #endregion

        #region "SendEmail"

        public ActionResult SendMail(int id)
        {
            var user = _userRepository.GetUserById(id);
            if (user != null)
            {
                return View(user);
            }
            else
            {
                ViewBag.NofFound = "4";
                return View();
            }
        }

        public ActionResult SendMail(FormCollection fc)
        {
            var userName = fc["userName"];
            var userModel = _userRepository.Get(u => u.UserName.Equals(userName));
            var title = fc["title"];
            var body = fc["body"];
            //Utils.getInstant().SendEmail(UserModel.UserName,UserModel.Email, EmailContent.RegisterTitle(),
            //                                 EmailContent.RegisterContent(usermodel.UserName, usermodel.ActiveCode.ToString()));
            return Content("1");
        }

        #endregion

    }
}
